package com.mall.service.mallGroupbBuy;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.mallGroupBuy.MallGroupbBuyService;
import com.mall.custom.dao.read.MallGroupBuyReadMapper;
import com.mall.custom.dao.write.MallGroupBuyWriteMapper;
import com.mall.pojo.MallGroupBuy;
import com.mall.pojo.MallGroupBuyExample;
import com.mall.pojo.MallGroupBuyExample.Criteria;
import com.mall.query.MallGroupBuyQuery;
import com.mall.query.Page;
import com.mall.searchdo.SearchResultIdAndCountDO;
import com.mall.searchdo.SearchResultIdDO;

@Service
public class MallGroupbBuyServiceImpl implements MallGroupbBuyService {

	@Autowired
	private MallGroupBuyReadMapper mallGroupBuyReadMapper;
	
	@Autowired
	private MallGroupBuyWriteMapper mallGroupBuyWriteMapper;

	@Override
	public List<MallGroupBuy> getMallGroupBuyListByExample(MallGroupBuyExample mallGroupBuyExample) {
		return this.mallGroupBuyReadMapper.selectByExample(mallGroupBuyExample);
	}

	@Override
	public MallGroupBuy getMallGroupBuyById(Long mallGroupBuyId) {
		return this.mallGroupBuyReadMapper.selectByPrimaryKey(mallGroupBuyId);
	}

	@Override
	public List<MallGroupBuy> getMallGroupBuyListByMallGroupBuyQuery(MallGroupBuyQuery mallGroupBuyQuery, Page page) {
		if (page == null) {
			page = Page.getInitPage();
		}
		MallGroupBuyExample mallGroupBuyExample = new MallGroupBuyExample();
		mallGroupBuyExample.setLimit(page.getLength());
		mallGroupBuyExample.setStart(page.getBegin());
		if (StringUtils.isNotBlank(mallGroupBuyQuery.getOrderby())) {
			mallGroupBuyExample.setOrderByClause(mallGroupBuyQuery.getOrderby());
		}
		Criteria criteria = mallGroupBuyExample.createCriteria();
		if (mallGroupBuyQuery.getOnlyKey() != null && mallGroupBuyQuery.getOnlyKey() > 0) {
			criteria.andCommOnlyKeyEqualTo(mallGroupBuyQuery.getOnlyKey());
		}
		if (mallGroupBuyQuery.getMallId() != null && mallGroupBuyQuery.getMallId() > 0) {
			criteria.andMallIdEqualTo(mallGroupBuyQuery.getMallId());
		}
		if (mallGroupBuyQuery.getStatus() != null && mallGroupBuyQuery.getStatus() > 0) {
			criteria.andStatusEqualTo(mallGroupBuyQuery.getStatus());
		}
		if (mallGroupBuyQuery.getType() != null && mallGroupBuyQuery.getType() > 0) {
			criteria.andTypeEqualTo(mallGroupBuyQuery.getType());
		}
		if (mallGroupBuyQuery.getIds() != null && mallGroupBuyQuery.getIds().size() > 0) {
			criteria.andIdIn(mallGroupBuyQuery.getIds());
		}
		if (StringUtils.isNotBlank(mallGroupBuyQuery.getKeyWord())) {
			criteria.andPyNameLike("%" + mallGroupBuyQuery.getKeyWord() + "%");
		}
		return this.getMallGroupBuyListByExample(mallGroupBuyExample);
	}

	@Override
	public List<MallGroupBuy> getMallGroupBuyList4ListByMallIds(List<Long> mallIds) {
		List<SearchResultIdAndCountDO> searchResultIdAndCountDOList = this.mallGroupBuyReadMapper
				.getMallGroupBuyList4ListByMallIds(mallIds);
		if (searchResultIdAndCountDOList == null || searchResultIdAndCountDOList.size() <= 0) {
			return null;
		}
		List<Long> idList = new ArrayList<>();
		for (SearchResultIdAndCountDO searchResultIdAndCountDO : searchResultIdAndCountDOList) {
			idList.add(searchResultIdAndCountDO.getId());
		}
		MallGroupBuyQuery mallGroupBuyQuery = new MallGroupBuyQuery();
		mallGroupBuyQuery.setIds(idList);
		return this.getMallGroupBuyListByMallGroupBuyQuery(mallGroupBuyQuery,
				Page.getInitPage(searchResultIdAndCountDOList.size()));
	}

	@Override
	public void modifyMallGroupBuyBuyByPrimaryKey(MallGroupBuy mallGroupBuy) {
		this.mallGroupBuyWriteMapper.updateByPrimaryKey(mallGroupBuy);
		
	}

	@Override
	public int insertMallGroupBuy(MallGroupBuy mallGroupBuy) {
		return this.mallGroupBuyWriteMapper.insertSelective(mallGroupBuy);
	}

	@Override
	public List<MallGroupBuy> getMallGroupBuyListByMallId(Long mallId, Integer type) {
		MallGroupBuyExample example = new MallGroupBuyExample();
		Criteria criteria = example.createCriteria();
		criteria.andMallIdEqualTo(mallId);
		criteria.andStatusEqualTo(0);
		if (type!=null) {
			criteria.andTypeEqualTo(type);
		}
		return this.mallGroupBuyReadMapper.selectByExample(example);
	}

}
